﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.BouncyCastle.Asn1;

namespace TheClerk.Forms.Templates.raw.Envelope
{
    /// <summary>
    /// The Container for Assertions and Commitments
    /// </summary>
    /// <remarks>
    /// AssertionCommitment ::= SEQUENCE {
    ///     assertions Assertions,
    ///     commitments Commitments }
    /// </remarks>
    internal class AssertionCommitment
    {
        internal Assertions assertions { get; private set; }
        internal Commitments commitments { get; private set; }

        /// <summary>
        /// Initialize with the Assertions object
        /// </summary>
        /// <param name="input">object containing every Assertion</param>
        internal AssertionCommitment()
        {
            assertions = new Assertions();
        }

        [Obsolete("Use member 'assertions' instead")]
        internal Assertions GetAssertions()
        {
            return assertions;
        }

        internal Stream Serialize()
        {
            Stream output = new MemoryStream();

            Stream assertionserial = assertions.Serialize();
            commitments = new Commitments(assertionserial);
            Stream commitmentserial = commitments.Serialize();
            var s = new DerSequenceGenerator(output);
            s.AddObject(Asn1Object.FromStream(assertionserial));
            s.AddObject(Asn1Object.FromStream(commitmentserial));
            s.Close();
            output.Position = 0;
            return output;
        }
        Asn1Encodable ToAsn1Encodable()
        {
            return Asn1Object.FromStream(Serialize());
        }

        internal Chop GetOuterChop()
        {
            return assertions.GetOuterChop();
        }

        internal void AddAssertion(Assertion content)
        {
            assertions.AddAssertion(content);
        }

        internal byte[] ToByteArray()
        {
            var s = Serialize();
            var b = new byte[s.Length];
            s.Read(b, 0, b.Length);
            return b;
        }
    }
}
